﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CodeGeneratorBootstrap.Models;
using CodeGeneratorBootstrap.ClassServices;
using CodeGeneratorBootstrap.Generators;

namespace CodeGeneratorBootstrap.Models
{
    public class AttributeModel
    {
        public int Id { get; set; }
        public ClassModel ClassModel { get; set; }
        public string Name { get; set; }
        public bool Display { get; set; }
        public bool Search { get; set; }
        public bool LinkEdit { get; set; }
        public string FieldDescription { get; set; }
        public string Label { get; set; }
        public int Length { get; set; }
        public bool Autokey { get; set; }
        public bool Nullable { get; set; }
        public bool Pk { get; set; }
        public int SeqNo { get; set; }
        public int DisplayLengthInGrid { get; set; }
        public DataTypeModel DataType { get; set; }
        public FieldTypeModel FieldType { get; set; }
        internal RelationshipModel RelationshipToParent { get; set; }
        public bool IsLinkToParent { get; set; }
        public ClassModel ChildClass { get; set; }
        public ClassModel ParentClass { get; set; }
        public ClassModel LookupClass { get; set; }
        public string Formula { get; set; }
        public string TransFormula { get; set; }
        
        //Weerachai 20140406
        public string DefaultValue { get; set; }
        public bool Readonly { get; set; }
        
        // Start add for Summary field
        public AttributeModel AttributeForSummary { get; set; }
        // End add for Summary field
        /// <summary>
        /// Name ::: Mr. Akawit Tangkupatanonth
        /// Date ::: 16/03/2014
        /// Description ::: Add new attribute {Validate, Formula}
        /// </summary>

        public bool Validate { get; set; }

        public string ValidateFormula { get; set; }
        public bool IsLookup
        {
            get
            {
                //if (this.DataType.Name.Equals("Lookup Table") || this.DataType.Name.Equals("Master Detail"))
                if (this.DataType.Name.Equals("Lookup Table"))
                    return true;
                else
                    return false;
            }
        }

        //Weerachai 27-11-2013
        public bool IsMasterDetail
        {
            get
            {
                if (this.DataType.Name.Equals("Master Detail"))
                    return true;
                else
                    return false;
            }
        }

        public bool IsPickupList
        {
            get
            {
                if (this.DataType.Name.Equals("Pickup List"))
                    return true;
                else
                    return false;
            }
        }

        public bool IsInt
        {
            get
            {
                if (this.DataType.Name.Equals("int"))
                    return true;
                else
                    return false;
            }
        }

        public AttributeModel() { }


        internal AttributeModel(AttributeModelDto dot)
        {
            if (dot != null)
            {
                Id = dot.Id;
                Name = dot.Name;
                Display = dot.Display;
                Search = dot.Search;
                LinkEdit = dot.LinkEdit;
                FieldDescription = dot.FieldDescription;
                Label = dot.Label == null ? dot.Name : dot.Label;
                Length = dot.Length;
                Autokey = dot.AutoKey;
                Nullable = dot.Nullable;
                Pk = dot.Pk;
                SeqNo = dot.SeqNo;
                DisplayLengthInGrid = dot.DisplayLengthInGrid;

                DataType = new DataTypeModel(dot.DataType);
                FieldType = new FieldTypeModel(dot.FieldType);
                Validate = dot.Validate;
                Formula = dot.Formula;
                ValidateFormula = dot.ValidateFormula;

                //Weerachai 20140406
                DefaultValue = dot.DefaultValue;
                Readonly = dot.Readonly;
                
            }
        }

        public bool EdtitLinkField
        {
            get
            {
                return this.Pk == true;
            }
        }

        private List<PickupListModel> pickuplists = null;

        public List<PickupListModel> GetPickupList()
        {
            if (this.pickuplists == null)
                this.pickuplists = Generator.GetPickupList(this);
            return this.pickuplists;
        }

        //Weerachai 23-12-2013
        private List<LookupModel> lookupModels = null;

        public List<LookupModel> GetLookupModel()
        {
            if (this.lookupModels == null)
                this.lookupModels = Generator.GetLookupModel(this);
            return this.lookupModels;
        }


        public LookupClassModel GetLookup(List<LookupClassModel> list)
        {
            foreach (LookupClassModel l in list)
                if (l.ForAttributeId == this.Id)
                    return l;
            return new LookupClassModel();
        }

        
    }
}
